package ta.structure;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class TADeclaration
{
    private final HashMap<String, ArrayList<String>> defs;

    public TADeclaration()
    {
        defs = new HashMap<>();
    }

    public void addVariable(String type, String def)
    {
        ArrayList<String> list;
        if (defs.containsKey(type))
            list = defs.get(type);
        else
        {
            list = new ArrayList<>();
            defs.put(type, list);
        }
        list.add(def);
    }

    public HashMap<String, ArrayList<String>> getDefs()
    {
        return defs;
    }

    public String generateDeclarations()
    {
        StringBuilder sb = new StringBuilder();
        for (var p : defs.entrySet())
        {
            var list = p.getValue();
            sb.append(String.format("%s %s;", p.getKey(), Arrays
                    .stream(list.toArray()).reduce((x, y) -> String.format("%s, %s", x, y)).get()));
        }
        return sb.toString();
    }
}
